function prepareForm(formElement){
	formElement.find('.phoneAreaCode,.phonePrefix,.phoneSuffix').click(function(){
		$("#content").RemoveBubblePopup();
	});
	formElement.find(".phoneAreaCode,.phonePrefix,.phoneSuffix").keydown(function(event){
		if($.inArray(event.keyCode,[46,8,9])!==-1||(event.keyCode==65&&event.ctrlKey===true)||(event.keyCode>=35&&event.keyCode<=39)){
			return;
		}else{
			if(event.shiftKey ||(event.keyCode < 48 || event.keyCode > 57)&&(event.keyCode < 96 || event.keyCode > 105)){
				event.preventDefault(); 
			}  
		}
	});
	formElement.find('.phoneAreaCode,.phonePrefix,.phoneSuffix').keyup(function(){
		$("#content").RemoveBubblePopup();
		// check pattern
		var pat;
		if($(this).attr("name")=="phoneAreaCode"){
			pat=/^[2-9][0-9]{2}$/;
		}else if($(this).attr("name")=="phonePrefix"){
			pat=/^[0-9]{3}$/;
		}else if($(this).attr("name")=="phoneSuffix"){
			pat=/^[0-9]{4}$/;
		}
		if(pat.test($(this).val())){
			$(this).css('color','');
		}else{
			$(this).css('color','#f00');
		}
		if(event.keyCode==8&&($(this).attr("name")=="phonePrefix"||$(this).attr("name")=="phoneSuffix")){
			if(this.selectionStart==0){
				$(this).prevAll('input').first().focus();
			}
		}
	});
	formElement.submit(function(){
		$("#content").RemoveBubblePopup();
		$('body').css('cursor','wait');
		var a=/^[2-9][0-9]{9}$/;
		var phone=$(this).children(".phoneAreaCode").val()+$(this).children(".phonePrefix").val()+$(this).children(".phoneSuffix").val();
		if(a.test(phone)){
			$.post(
				'./',
				$(this).serialize(),
				function(data){
					$("#content").CreateBubblePopup({innerHtml:data,innerHtmlStyle:{'color':'#000000'},position:'bottom',themeName:'all-yellow'});
					$("#content").ShowBubblePopup();
					$("#content").FreezeBubblePopup();
					$('body').css('cursor','');
				}
			);
		}else{
			$("#content").CreateBubblePopup({innerHtml:"Please enter your phone number in this format: XXX-XXX-XXXX.",innerHtmlStyle:{'color':'#000000'},position:'bottom',themeName:'all-yellow'});
			$("#content").ShowBubblePopup();
			$("#content").FreezeBubblePopup();
			$('body').css('cursor','');
		}
		return false;
	});   
};
$(document).ready(function(){
	prepareForm($("#phoneForm"));
	setTimeout(function(){
		var lastleft=Math.floor(($('body').width()-800)/2);
		var lastTop=Math.floor(($('.cb-slideshow').height()-300-$("#broughtBy").height())/2);
		$(".cb-slideshow li:nth-child(14) span").css({
			'opacity':'1',
		});
		$(".container").fadeOut(1500);
		$("#broughtBy").css({
			'left':Math.floor(($('body').width()-$("#broughtBy").width())/2),
			'top':lastTop+300,
		}).fadeIn(1500);
		$("#content").fadeOut(1500,function(){
			$("#content").css({
				'left':lastleft,
				'right':'auto',
				'top':lastTop+300-$("#content").height(),
			});
		}).fadeIn(1500);
		$('body').append('<iframe frameborder="0" scrolling="no" src="demo_converting_presentations/vid.php" style="left:'+(lastleft+370)+'px;top:'+(lastTop+15)+'px;"></iframe>');
	},84000);
});
$(window).resize(function(){
	if($("#broughtBy").css('display')=='block'){
		var lastleft=Math.floor(($('body').width()-800)/2);
		var lastTop=Math.floor(($('.cb-slideshow').height()-300-$("#broughtBy").height())/2);
		$("#broughtBy").css({
			'left':Math.floor(($('body').width()-$("#broughtBy").width())/2),
			'top':lastTop+300,
		});
		$("#content").css({
			'left':lastleft,
			'top':lastTop+300-$("#content").height(),
		});
		$('iframe').css({
			'left':lastleft+370,
			'top':lastTop+15,
		});
	};
});

